package com.neu.cloudfactory.service.imp;

import com.neu.cloudfactory.bean.Equipement.Equipment;
import com.neu.cloudfactory.bean.Equipement.Equipmentstatus;
import com.neu.cloudfactory.bean.Equipement.Equipmenttype;
import com.neu.cloudfactory.repository.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.NoSuchElementException;

@Service
public class Equipmentserviceimpl {
    @Autowired
    EquipmenttypeRepository equipmenttypeRepository;
    @Autowired
    EquipmentRepository equipmentRepository;
    @Autowired
    EquipmentstatusRepository equipmentstatusRepository;
    @Autowired
    FactoryRepository factoryRepository;
    @Autowired
    LoanRepository loanRepository;

    public Page<Equipment> findAll( Integer page,  Integer size) {
        Pageable pageable = PageRequest.of(page-1,size);
        return equipmentRepository.findAll(pageable);
    }

    public String addEquipment( Equipment equipment){
        return equipmentRepository.saveAndFlush(equipment).getId()==null?"failed":"success";
    }

    public String delete(  Integer id){
        try{
            Equipment equipment = equipmentRepository.findById(id).get();
            equipmentRepository.delete(equipment);
            return "success";
        }catch (NoSuchElementException e){
            return "failed";
        }
    }

    public Equipment findById(  Integer id){
        return equipmentRepository.findById(id).get();
    }

    public Page<Equipment> findAllByFactoryid( Integer page,  Integer size,  Integer factoryid){
        Pageable pageable = PageRequest.of(page-1,size);
        try{
            return equipmentRepository.findAllByFactoryid(factoryid,pageable);
        }catch (Exception e){
            return null;
        }
    }

    public String Return( Integer id){
        Equipment equipment;
        boolean complete = false;
        try{
            equipment = equipmentRepository.getById(id);
            equipment.setFactoryid(0);
            equipment = equipmentRepository.save(equipment);

            loanRepository.deleteAllByEquipmentid(equipment.getId());

            complete=equipment.getFactoryid()==0;
        }catch (Exception e){
            System.out.println(e);
        }
        return complete?"success":"failed";
    }

    public String update( Equipment equipment){
        if(equipment.getId()!=null){
            equipment = equipmentRepository.save(equipment);
            return "success";
        }else{
            return "failed";
        }
    }

    public Equipment changeStaus( Integer id,  Integer statusid){
        Equipment equipment = equipmentRepository.findById(id).get();
        if(equipment!=null){
            switch (statusid){
                case 1:
                    equipment.setStatusid(3);
                    break;
                case 3:
                    equipment.setStatusid(1);
                    break;
                default:
                    break;
            }
            equipmentRepository.save(equipment);
            return equipment;
        }
        return null;
    }

    public Page<Equipment> factorysearch( String input, Integer page,  Integer size, Integer factoryid){
        Pageable pageable = PageRequest.of(page-1,size);
        int id;
        try {
            id = Integer.parseInt(input);
        }catch (Exception e){
            id = -1;
        }
        return equipmentRepository.findAllByIdOrNameContainingOrDescriptionContainingOrFactoryid(id,input,input,factoryid,pageable);
    }
    public Page<Equipment> search( String input, Integer page,  Integer size){
        Pageable pageable = PageRequest.of(page-1,size);
        int id;
        try {
            id = Integer.parseInt(input);
        }catch (Exception e){
            id = -1;
        }
        return equipmentRepository.findAllByIdOrNameContainingOrDescriptionContainingOrFactoryid(id,input,input,id,pageable);
    }

    public String addTtype( Equipmenttype equipmenttype){
        return equipmenttypeRepository.saveAndFlush(equipmenttype).getId()==null?"failed":"success";
    }

    public Page<Equipmenttype> typefindAll( Integer page,  Integer size) {
        Pageable pageable = PageRequest.of(page-1,size);
        return equipmenttypeRepository.findAll(pageable);
    }

    public Equipmenttype findtypeById(  Integer id){
        return equipmenttypeRepository.findById(id).get();
    }

    public String update( Equipmenttype equipmenttype){
        if(equipmenttype.getId()!=null){
            equipmenttypeRepository.save(equipmenttype);
            return "success";
        }else{
            return "failed";
        }
    }

    public String deletetype(  Integer id){
        try{
            Equipmenttype equipmenttype = equipmenttypeRepository.findById(id).get();
            equipmenttypeRepository.delete(equipmenttype);
            return "success";
        }catch (NoSuchElementException e){
            return "failed";
        }
    }

    public Page<Equipmenttype> typesearch( String input,  Integer page,  Integer size){
        Pageable pageable = PageRequest.of(page-1,size);
        int id;
        try {
            id = Integer.parseInt(input);
        }catch (Exception e){
            id = -1;
        }
        return equipmenttypeRepository.findAllByIdOrTypenameContainingOrDescriptionContaining(id,input,input,pageable);
    }

    public Page<Equipmentstatus> statusfindAll( Integer page,  Integer size) {
        Pageable pageable = PageRequest.of(page-1,size);
        return equipmentstatusRepository.findAll(pageable);
    }

    public Page<Equipmentstatus> statussearch( String input,  Integer page,  Integer size){
        Pageable pageable = PageRequest.of(page-1,size);
        int id;
        try {
            id = Integer.parseInt(input);
        }catch (Exception e){
            id = -1;
        }
        return equipmentstatusRepository.findAllByIdOrStatusContaining(id,input,pageable);
    }
}
